<template>
  <div class="system-role-dialog-container">
    <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="600px">
      <el-form ref="roleDialogFormRef" :model="state.ruleForm" :rules="state.formRules" size="default" label-width="120px"
        v-loading="state.dataLoading" label-position="right" style="min-height: 50px;">
        <el-form-item label="ID">
          <el-input v-model="state.ruleForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="state.ruleForm.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="手术级别" prop="level">
          <el-select v-model="state.ruleForm.level" placeholder="请选择" size="default">
            <el-option label="一级手术" value="1"></el-option>
            <el-option label="二级手术" value="2"></el-option>
            <el-option label="三级手术" value="3"></el-option>
            <el-option label="四级手术" value="4"></el-option>
            <el-option label="五级手术" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否准入">
          <el-radio-group v-model="state.ruleForm.isDaySign">
            <el-radio :label="true" size="default">是</el-radio>
            <el-radio :label="false" size="default">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="科室" prop="departmentIds">
          <el-select v-model="state.ruleForm.departmentIds" placeholder="请选择科室" size="default" clearable filterable
            multiple style="width: 100%;">
            <el-option v-for="(item, index) in state.departmentList" :key="index" :label="item.departmentName"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拼音助记符">
          <el-input v-model="state.ruleForm.py" placeholder="请输入拼音助记符"></el-input>
        </el-form-item>
        <el-form-item label="五笔助记符">
          <el-input v-model="state.ruleForm.wb" placeholder="请输入五笔助记符"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" :loading="state.btnLoading" @click="onSubmit" size="default">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="operationDialog">
import { entryAndExitApi } from '/@/api/entryAndExit'
import { ElMessage } from 'element-plus';
import { toolsApi } from '/@/api/tools'
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
  ruleForm: {
    id: '',
    name: '',
    level: '1',
    isDaySign: true,
    departmentIds: [],
    py: '',
    wb: ''
  },
  dialog: {
    isShowDialog: false,
    title: '术式字典-修改',
    submitTxt: '',
  },
  dataLoading: false,
  roleList: [],
  btnLoading: false,
  departmentList: [],
  formRules: {
    name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
    departmentIds: [{ required: true, message: '请选择科室', trigger: 'change' }]
  }
});

// 页面加载时
onMounted(() => {
  getDepatment()
});

// 打开弹窗
const openDialog = (row) => {
  const departmentIds = []
  row.department.forEach(item => {
    departmentIds.push(item.id)
  })
  state.ruleForm.id = row.id;
  state.ruleForm.name = row.name;
  state.ruleForm.level = row.level;
  state.ruleForm.isDaySign = row.isDaySign;
  state.ruleForm.departmentIds = departmentIds;
  state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
  state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};
// 提交
const onSubmit = () => {
  roleDialogFormRef.value.validate((valid, fields) => {
    if (valid) {
      state.btnLoading = true
      const apiData = JSON.parse(JSON.stringify(state.ruleForm))
      entryAndExitApi().operationUpdate(apiData).then(res => {
        state.btnLoading = false
        ElMessage.success('操作成功');
        closeDialog();
        emit('refresh');
      }).catch(() => {
        state.btnLoading = false
        closeDialog();
        emit('refresh');
      })
    }
  })




};

// 获取科室
const getDepatment = () => {
  toolsApi().departmentList({ all: 1 }).then(res => {
    state.departmentList = res.data
  })
}


// 暴露变量
defineExpose({
  openDialog,
});
</script>

<style scoped lang="scss"></style>
